<template>
  <div class="xtx-carousel" @mouseenter="stop" @mouseleave="start">
    <!-- 轮播列表 -->
    <ul class="carousel-body">
      <li class="carousel-item" :class="{ fade: i === index }" v-for="item, i in props.banners" :key="item.id">
        <RouterLink to="/">
          <img :src="item.imgUrl" alt />
        </RouterLink>
      </li>
    </ul>
    <!-- 切换按钮 -->
    <a href="javascript:;" @click="switchIndex(-1)" class="carousel-btn prev"><i class="iconfont icon-angle-left"></i></a>
    <a href="javascript:;" @click="switchIndex(1)" class="carousel-btn next"><i class="iconfont icon-angle-right"></i></a>
    <!-- 分页器 -->
    <div class="carousel-indicator">
      <span @click="index = i" :class="{ active: i == index }" v-for="item, i in banners" :key="item.id"></span>
    </div>
  </div>
</template>

<script setup>
import { onUnmounted, ref, watch } from 'vue'

const props = defineProps({
  banners: { // banners
    type: Array,
    default: () => []
  },
  autoPlay: { // 是否自动播放
    type: Boolean,
    default: false
  },
  interval: { // 切换间隔(单位：毫秒)
    type: Number,
    default: 3000
  },
  current: {
    type: Number,
    default: 0
  }
})

// 当前索引
const index = ref(props.current)
// 定时器ID
let timer = null

// 设置自动播放
const startAutoPlay = () => {
  clearInterval(timer)
  timer = setInterval(() => {
    // 每隔3s切换一张
    index.value = index.value === props.banners.length - 1 ? 0 : index.value + 1
  }, props.interval)
}

// 监听自动播放 实现开启和关闭
watch(() => props.autoPlay, (v) => {
  if (v) {
    startAutoPlay()
  } else {
    clearInterval(timer)
  }
}, { immediate: true })

// 按钮切换
const switchIndex = (n) => {
  const currentIndex = index.value + n
  if (currentIndex === props.banners.length) {
    // 判断是否为最后一张
    index.value = 0
  } else if (currentIndex < 0) {
    // 判断是否为第一张
    index.value = props.banners.length - 1
  } else {
    index.value = currentIndex
  }
}

// 开始轮播
const start = () => {
  props.banners.length && props.autoPlay && startAutoPlay()
}

// 暂停轮播
const stop = () => {
  clearInterval(timer)
}

// 组件销毁后 清理定时器
onUnmounted(() => {
  timer && clearInterval(timer)
})
</script>

<style lang="scss" scoped>
.xtx-carousel {
  width: 100%;
  height: 100%;
  position: relative;
  min-width: 300px;
  min-height: 150px;
  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0,0,0,.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev{
        left: 20px;
      }
      &.next{
        right: 20px;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0,0,0,0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background:  #fff;
        }
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>
